package com.ant.storage.controller;

import com.ant.storage.common.ResultCode;
import com.ant.storage.common.ResultPage;
import com.ant.storage.common.ResultVo;
import com.ant.storage.dto.InventoryDto;
import com.ant.storage.dto.WarehouseDto;
import com.ant.storage.handler.AntException;
import com.ant.storage.pojo.UserInfo;
import com.ant.storage.pojo.Warehouse;
import com.ant.storage.service.IInventoryService;
import com.ant.storage.service.IWarehouseService;
import com.ant.storage.vo.CommodityVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author melon
 * @since 2022-03-16
 */
@RestController
@RequestMapping("/warehouse")
@Api(tags = "仓库接口")
public class WarehouseController {

    @Autowired
    private IWarehouseService warehouseService;

    @Autowired
    private IInventoryService inventoryService;

    @Autowired
    public ServletContext servletContext;

    @ApiOperation(value = "获取仓库分页信息")
    @GetMapping("/getWarehousePage")
    public ResultPage getWarehousePage(WarehouseDto warehouseDto) {
        return ResultPage.success("获取成功", warehouseService.getWarehousePage(warehouseDto));
    }

    @ApiOperation(value = "获取仓库树列表信息")
    @GetMapping("/getWarehouseTreeList")
    public ResultVo getWarehouseTreeList()
    {
        List<Warehouse> list = warehouseService.list();
        List<Warehouse> Departments = list.stream()
                .filter(e -> e.getParentId()==0)
                .map(e -> {
                    e.setChildren(getTreeChildren(e, list));
                    return e;
                }).collect(Collectors.toList());
        return ResultVo.success("获取成功",Departments);
    }

    private List<Warehouse> getTreeChildren(Warehouse e, List<Warehouse> list) {
        List<Warehouse> children = list.stream()
                .filter(m -> Objects.equals(e.getParentId(), m.getParentId()))
                .map(m -> {
                    m.setChildren(getTreeChildren(m, list));
                    return m;
                }).collect(Collectors.toList());
        return children;
    }


    @ApiOperation(value = "查看仓库出入记录")
    @GetMapping("/findWarehouseAccessRecord")
    public ResultVo findWarehouseAccessRecord(WarehouseDto dto) {
        return ResultVo.success("获取成功", warehouseService.findWarehouseAccessRecord(dto));
    }

    @ApiOperation(value = "通城市 id 获取仓库列表")
    @GetMapping("/getWarehouseList")
    public ResultVo getWarehouseList(@RequestParam("cityId") String cityId) {
        return ResultVo.success("获取成功", warehouseService.getWarehouseList(cityId));
    }


    @ApiOperation(value = "通城市id获取部门仓库列表")
    @GetMapping("/getWarehouseDeptByIdList/{id}")
    public ResultVo getWarehouseDeptByIdList(@PathVariable("id") String id) {
        return ResultVo.success("获取成功",
                warehouseService.list(new QueryWrapper<Warehouse>().eq("dept_id", id)));
    }


    @ApiOperation(value = "通仓库id获取物品列表信息")
    @GetMapping("/getWarehouseDeptList")
    public ResultVo getWarehouseDeptList(WarehouseDto dto) {
        if (dto.getWarehouseId() == null) {
            throw new AntException("id不能为空");
        }
        return warehouseService.getWarehouseDeptList(dto);
    }

    @ApiOperation(value = "通仓库id获取物品列表信息")
    @GetMapping("/getWarehouseListes")
    public ResultVo getWarehouseListes(WarehouseDto dto) {
        if (dto.getWarehouseId() == null) {
            throw new AntException("id不能为空");
        }
        return ResultVo.success("获取成功", warehouseService.getWarehouseListes(dto));
    }

    @ApiOperation(value = "通仓库id获取物品列表信息2")
    @GetMapping("/getInventoryByWarehouseId")
    public ResultVo getInventoryByWarehouseId(InventoryDto dto) {
        Map<String,Object> map= warehouseService.getInventoryByWarehouseId(dto);
        return ResultVo.success("获取成功", map);
    }

    @ApiOperation(value = "通仓库id获取物品列表信息2")
    @GetMapping("/getWarehouseAndInventoryById/{id}")
    public ResultVo getWarehouseAndInventoryById(@PathVariable("id") Integer id) {
        List<CommodityVo> list = inventoryService.findWarehouseAndInventory(id);
        return ResultVo.success("获取成功", list);
    }

    @ApiOperation(value = "添加仓库")
    @PostMapping("/addWarehouse")
    public ResultVo addWarehouse(@RequestBody @Validated Warehouse warehouse, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new AntException(bindingResult.getFieldError().getDefaultMessage());
        }
        if (StringUtils.isEmpty(warehouse.getWarehouseName())) {
            throw new AntException(ResultCode.PARAM_IS_BLANK.getResultCode(), "名称不能为空");
        } else {

            Warehouse one = warehouseService.getOne(new QueryWrapper<Warehouse>()
                    .eq("parent_id", 0)
                    .eq("id",warehouse.getId())
                    .eq("city_id", warehouse.getCityId()));
            UserInfo userInfo = (UserInfo) servletContext.getAttribute("userInfo");
            warehouse.setCreatorBy(userInfo.getUserName());
            warehouse.setCreationTime(new Date());
            if (one==null) {
                warehouseService.save(warehouse);
                return ResultVo.success("添加成功");
            } else {
                warehouseService.updateById(warehouse);
                return ResultVo.success("修改成功");
            }
        }
    }


    @ApiOperation(value = "获取车辆仓库库存物资")
    @GetMapping("/getWarehouseCarInventoryById/{id}")
    public ResultVo getWarehouseCarInventoryById(@PathVariable("id") Integer id) {
        List<CommodityVo> list = inventoryService.findWarehouseCarInventory(id);
        return ResultVo.success("获取成功", list);
    }
}
